using System.Reflection;
using UnityEngine;

namespace CinemaDirector
{
	[CutsceneItem("Game Object", "Enable Behaviour")]
	public class EnableBehaviour : CinemaActorEvent
	{
		public Component Behaviour;

		private bool cachedState;

		public override void Initialize(GameObject actor)
		{
			if (actor != null)
			{
				Component component = actor.GetComponent(Behaviour.GetType());
				if (component != null)
				{
					PropertyInfo property = Behaviour.GetType().GetProperty("enabled");
					cachedState = (bool)property.GetValue(component, null);
				}
			}
		}

		public override void Trigger(GameObject actor)
		{
			Component component = actor.GetComponent(Behaviour.GetType());
			if (component != null)
			{
				PropertyInfo property = Behaviour.GetType().GetProperty("enabled");
				property.SetValue(Behaviour, true, null);
			}
		}

		public override void Reverse(GameObject actor)
		{
			Component component = actor.GetComponent(Behaviour.GetType());
			if (component != null)
			{
				PropertyInfo property = Behaviour.GetType().GetProperty("enabled");
				property.SetValue(component, false, null);
			}
		}

		public override void Stop(GameObject actor)
		{
			if (actor != null)
			{
				Component component = actor.GetComponent(Behaviour.GetType());
				if (component != null)
				{
					PropertyInfo property = Behaviour.GetType().GetProperty("enabled");
					property.SetValue(component, cachedState, null);
				}
			}
		}
	}
}
